package com.ss.android.article.base.feature.feed.presenter;

import android.content.SharedPreferences;
import android.os.Message;
import android.text.TextUtils;

import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.category.a.CategoryManager;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

final class p implements Runnable {
    private final RefreshAdManager a;

    p(RefreshAdManager arg1) {
        this.a = arg1;
    }

    public final void run() {
        Object v2_4;
        long v4;
        RefreshAdManager.b v3_1;
        String v0_2;
        RefreshAdManager v1 = this.a;
        try {
            ArrayList v0_1 = new ArrayList();
            CategoryManager v2 = CategoryManager.getInstance(v1.mContext);
            Map v3 = v2.c;
            if (v3.isEmpty()) {
                MobClickCombiner.onEvent(v2.mContext, "category", "get_subscribe_empty");
            }

            String availableCategory = CategoryManager.list2Str(v3.keySet());
            if (!android.support.design.a.isTextEmpty(availableCategory)) {
                v0_1.add(new NameValueImpl("available_category", availableCategory));
            }

            if (v0_1.isEmpty()) {
                return;
            }

            v0_2 = NetworkUtils.executePost(-1, ArticleConstants.serviceRefreshAdUrl, ((List) v0_1));
            if (android.support.design.a.isTextEmpty(v0_2)) {
                return;
            }

            JSONObject v2_2 = new JSONObject(v0_2);
            if (!AbsApiThread.isApiSuccess(v2_2)) {
                return;
            }

            v1.mLastFetchTime = System.currentTimeMillis();
            JSONObject v0_3 = v2_2.getJSONObject("data");
            if (v0_3 == null) {
                return;
            }

            JSONArray v2_3 = v0_3.optJSONArray("ad_item");
            v3_1 = new RefreshAdManager.b();
            v3_1.a(v2_3);
            v4 = v1.mLastFetchTime;
            v0_2 = "";
            if (v2_3 != null) {
                v0_2 = v2_3.toString();
            }

            v2_4 = RefreshAdManager.mLock;
            synchronized (v2_4) {
                SharedPreferences.Editor v6 = v1.mSharedPreferences.edit();
                if (!TextUtils.isEmpty(((CharSequence) v0_2))) {
                    v6.putString("pull_refresh_ad_str", v0_2);
                }
                v6.putLong("last_fetch_time", v4);
                SharedPreferenceCompat.commit(v6);
            }
            Message message = v1.mWeakHandler.obtainMessage(10);
            message.obj = v3_1;
            v1.mWeakHandler.sendMessage(message);
        } catch(Exception v0_4) {
        }


    }
}

